<template>
  <div class="ebook-slide-contents">
    <!-- search -->
    <div class="slide-contents-search-wrapper">
      <div class="slide-contents-search-input-wrapper">
        <div class="slide-contents-search-icon">
          <span class="icon-search"></span>
        </div>
        <input
          type="text"
          class="slide-contents-search-input"
          :placeholder="$t('book.searchHint')"
          @click="showSearchPage()"
          @keyup.enter="search()"
          v-model="searchText"
        />
      </div>
      <div
        class="slide-contens-search-cancel"
        v-if="searchVisible"
        @click="hideSearchPage()"
      >{{ $t('book.cancel') }}</div>
    </div>
    <!-- 图书 -->
    <div class="slide-contents-book-wrapper" v-show="!searchVisible">
      <div class="slide-contents-book-img-wrapper">
        <img :src="cover" alt class="slide-contents-book-img" />
      </div>
      <div class="slide-contents-book-info-wrapper">
        <div class="slide-contents-book-title">{{ metadata ? metadata.title:'' }}</div>
        <div class="slide-contents-book-author">{{ metadata ? metadata.creator:'' }}</div>
      </div>
      <div class="slide-contents-book-progress-wrapper">
        <div class="slide-contents-book-pregress">
          <span class="progess">{{ progress + '%'}}</span>
          <span class="progress-text">{{$t('book.haveRead2')}}</span>
        </div>
        <div class="slide-contents-book-time">{{ getReadTimeText() }}</div>
      </div>
    </div>
    <scroll
      class="slide-contents-list"
      :top="156"
      :bottom="80"
      v-show="!searchVisible"
      ref="scroll"
    >
      <div class="slide-contents-item" v-for="(item, index) in navigation" :key="index">
        <span
          class="slide-contents-item-label"
          @click="displayNavigation(item.href, false)"
          :class="{'selected': section === index}"
          :style="contentItemStyle(item)"
        >{{ item.label }}</span>
        <span class="slide-contents-item-page"></span>
      </div>
    </scroll>
    <!-- 搜索结果列表 -->
    <scroll class="slide-search-list" :top="66" :bottom="48" v-show="searchVisible">
      <div
        class="slide-search-item"
        v-for="(item, index) in searchList"
        :key="index"
        @click="displayNavigation(item.cfi, true)"
        v-html="item.excerpt"
      ></div>
    </scroll>
  </div>
</template>

<script>
import Scroll from "../common/Scroll";
import { ebookMixin } from "../../utils/mixin";
import { px2rem } from "../../utils/utils";
export default {
  name: "",
  mixins: [ebookMixin],
  components: {
    Scroll
  },
  data() {
    return {
      searchVisible: false,
      searchText: null,
      searchList: null
    };
  },
  methods: {
    search() {
      // 搜索
      if (this.searchText && this.searchText.length > 0) {
        this.doSearch(this.searchText).then(list => {
          this.searchList = list;
          this.searchList.map(item => {
            item.excerpt = item.excerpt.replace(this.searchText,`<span class="content-search-text">${this.searchText}</span>`)
            return item
          })
        });
      }
    },
    doSearch(q) { // github 上面的技巧 https://github.com/futurepress/epub.js/wiki/Tips-and-Tricks-%28v0.3%29
      return Promise.all(
        this.currentBook.spine.spineItems.map(item =>
          item
            .load(this.currentBook.load.bind(this.currentBook))
            .then(item.find.bind(item, q))
            .finally(item.unload.bind(item))
        )
      ).then(results => Promise.resolve([].concat.apply([], results)));
    },
    displayNavigation(target, flag) {
      this.display(target, () => {
        this.hideTitleAndMenu();
        if (flag) {
          this.currentBook.rendition.annotations.highlight(target)
        }
      });
    },
    contentItemStyle(item) {
      return {
        marginLeft: `${px2rem(item.level * 15)}rem`
      };
    },
    showSearchPage() {
      this.searchVisible = true;
    },
    hideSearchPage() {
      // 隐藏搜索框
      this.searchVisible = false;
      this.searchText = "";
      this.searchList = null;
    }
  }
};
</script>

<style lang='scss' scoped>
@import "../../assets/styles/global.scss";
.ebook-slide-contents {
  width: 100%;
  font-size: 0;
  .slide-contents-search-wrapper {
    display: flex;
    width: 100%;
    height: px2rem(36);
    margin: px2rem(20) 0 px2rem(10) 0;
    box-sizing: border-box;
    .slide-contents-search-input-wrapper {
      flex: 1;
      @include center;
      .slide-contents-search-icon {
        flex: 0 0 px2rem(28);
        font-size: px2rem(12);
        @include center;
      }
      .slide-contents-search-input {
        flex: 1;
        width: 100%;
        height: px2rem(32);
        background: transparent;
        border: none;
        &:focus {
          outline: none;
        }
      }
    }
    .slide-contens-search-cancel {
      flex: 0 0 px2rem(50);
      font-size: px2rem(14);
      @include right;
    }
  }

  .slide-contents-book-wrapper {
    display: flex;
    width: 100%;
    height: px2rem(90);
    padding: px2rem(10) px2rem(15) px2rem(20) px2rem(15);
    .slide-contents-book-img-wrapper {
      flex: 0 0 px2rem(45);
      .slide-contents-book-img {
        width: px2rem(45);
        height: px2rem(60);
      }
    }
    .slide-contents-book-info-wrapper {
      flex: 1;
      padding: 0 px2rem(10);
      box-sizing: border-box;
      .slide-contents-book-title {
        font-size: px2rem(14);
        width: px2rem(153.75);
        line-height: px2rem(16);
        @include ellipsis2(2);
      }
      .slide-contents-book-author {
        width: px2rem(153.75);
        font-size: px2rem(12);
        margin-top: px2rem(5);
        @include ellipsis;
      }
    }
    .slide-contents-book-progress-wrapper {
      flex: 0 0 px2rem(70);
      .slide-contents-book-pregress {
        .progess {
          font-size: px2rem(14);
        }
        .progress-text {
          font-size: px2rem(12);
        }
      }
      .slide-contents-book-time {
        font-size: px2rem(12);
        margin-top: px2rem(5);
      }
    }
  }
  .slide-contents-list {
    padding: 0 px2rem(15);
    box-sizing: border-box;
    .slide-contents-item {
      display: flex;
      padding: px2rem(20) 0;
      box-sizing: border-box;
      .slide-contents-item-label {
        flex: 1;
        font-size: px2rem(14);
        @include ellipsis;
      }
      // .slide-contents-item-page {
      // }
    }
  }
  .slide-search-list {
    width: 100%;
    padding: 0 px2rem(15);
    box-sizing: border-box;
    .slide-search-item {
      font-size: px2rem(14);
      padding: 0 px2rem(14);
      line-height: px2rem(16);
      padding: px2rem(20) 0;
      box-sizing: border-box;
    }
  }
}
</style>
